﻿/* 
 * -----------------------------------------------------------------------
 * <copyright file="AccountManagementListShowChild.xaml.cs" company="O-RID">
 *     Copyright (c)2010 O-RID Co.,Ltd.
 * </copyright>
 * <author>Zhang jun</author>
 * <summary>
 *              帐号管理子窗体
 * 引用服务：WCFServers.WCFAccountsRefy>
 * 服务功能：数据查询 
 *           引用服务：WCFLog4Ref.WCLog4Client
 *           服务功能：记录日志
 *        作成日：05/21/2010
 * </summary>
 * -----------------------------------------------------------------------
 */

namespace Orid.Management.Statistics.OperatorUnitMangement.CompetenceSetPaeg.AccountManagement
{
    using System;
    using System.Collections.ObjectModel;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Input;
    using System.Windows.Media;
    using Orid.Management.Statistics.OperatorUnitMangement.Common.FunctionList;
    using Orid.Management.Statistics.OperatorUnitMangement.WCFServers.WCFAccountsRef;
    using childWindow = Orid.Management.Statistics.OperatorUnitMangement.Common.Window;
    using Wcf = Orid.Management.Statistics.OperatorUnitMangement.Common.Service;

    /// <summary>
    /// 帐号管理子窗体
    /// AccountManagementListShowChild
    /// </summary>
    public partial class AccountManagementListShowChild : ChildWindow
    {
        /// <summary>
        /// 当前应用
        /// appConfig
        /// </summary>
        private App appConfig = (App)Application.Current;
 
        /// <summary>
        /// 消息子窗口
        /// messbox
        /// </summary>
        private childWindow.MessageBox messbox = new childWindow.MessageBox();
 
        /// <summary>
        /// 日志服务定义
        /// wcfLogClient
        /// </summary>
        private WCFLog4Ref.WCFLog4Client wcfLogClient;
 
        /// <summary>
        /// 分页控件
        ///    pcv
        /// </summary>
        private PagedCollectionView pcv;

        /// <summary>
        /// grid中行数
        /// pageCount
        /// </summary>
        private int pageCount = 10;
         
        /// <summary>
        /// ObservableCollection
        /// 当数据增加或册除时，同时更新。
        /// 为dagagrid的checkbox增加了一个CHECK状态字段
        /// </summary>
        private ObservableCollection<Client_AccountManagementDataList> clientList = new ObservableCollection<Client_AccountManagementDataList>();
      
        /// <summary>
        /// 构造函数
        /// AccountManagementListShowChild
        /// </summary>
        public AccountManagementListShowChild()
        {
            InitializeComponent();

            // 引用主题
            ((App)Application.Current).SetTheme(LayoutRoot);

            this.dgAMList.HeadersVisibility = DataGridHeadersVisibility.All;//同时显示DataGrid的行头与列头

            this.wcfLogClient = Wcf.Service.Log4; 
        } 

        /// <summary>
        /// 确定按钮
        /// OKButton_Click
        /// </summary>
        /// <param name="sender">sender of object</param>
        /// <param name="e">e type of RoutedEventArgs</param>
        private void OKButton_Click(object sender, RoutedEventArgs e)
        {          
            #region   将选中项加入参数列表中

            foreach (Client_AccountManagementDataList lst in this.clientList)
            {
                if (lst.AccountIDCheck)
                {
                    bool addAccountFlag = false;

                    if (ClientDataList.ParameterAccountList.Count != 0)
                    {
                        int index = ClientDataList.ParameterAccountList.IndexOf(lst.AccountID);
                        if (index == -1)
                        {
                            addAccountFlag = true;
                        }
                    }
                    else
                    {
                        addAccountFlag = true;
                    }

                    if (addAccountFlag)
                    {
                        // 把用户ID和用户名，一起添加
                        ClientDataList.ParameterAccountList.Add(lst.AccountID.Trim()+","+lst.OpName.Trim());
                    }
                }
            }
            #endregion    将选中项加入参数列表中

            this.DialogResult = true;
            this.clientList.Clear();
            this.dgAMList.DataContext = null;
        }

        /// <summary>
        /// 取消按钮
        /// CancelButton_Click
        /// </summary>
        /// <param name="sender">sender of object</param>
        /// <param name="e">e type of CancelButton_Click</param>
        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
            this.clientList.Clear();
            this.dgAMList.DataContext = null;
        }

        #region 搜索框KeyDown事件

        /// <summary>
        /// 搜索框KeyDown事件
        /// TbInput_KeyDown
        /// </summary>
        /// <param name="sender">sender of object</param>
        /// <param name="e">e type of KeyEventArgs</param>
        private void TbInput_KeyDown(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.Key == Key.Enter)
                {
                    WCFAccountsClient client = new WCFAccountsClient();

                    client.AccountManagementAllCompleted -= new EventHandler<AccountManagementAllCompletedEventArgs>(this.Client_AccountManagementAll);
                    client.AccountManagementAllCompleted += new EventHandler<AccountManagementAllCompletedEventArgs>(this.Client_AccountManagementAll);

                    if (string.IsNullOrEmpty(this.tbInput.Text))
                    {
                        client.AccountManagementAllAsync("%");
                    }
                    else
                    {
                        client.AccountManagementAllAsync(this.tbInput.Text.Trim());
                    }
                }
            }
            catch (Exception ex)
            {
                this.wcfLogClient.ErrorAsync(((App)Application.Current).LogStr + "AccountManagementListShowChild" + ex.Message.ToString());
                Common.FunctionList.FunctionClass.ShowMessageBox(
                  this.messbox,
                  1,
                  "提示",
                  ex.Message.ToString(),
                  Colors.Gray,
                  true,
                  Visibility.Visible,
                  Visibility.Collapsed);
                return;
            }
        }
        #endregion 搜索框KeyDown事件

        #region  单击搜索框，向DATAGRID中添加信息
        /// <summary>
        /// 单击搜索框，向DATAGRID中添加信息
        /// 将服务器端的LIST转换为客户端的ObservableCollection
        /// </summary>
        /// <param name="sender">sender of object</param>
        /// <param name="e">e type of AccountManagementAllCompletedEventArgs</param>
        private void Client_AccountManagementAll(object sender, AccountManagementAllCompletedEventArgs e)
        {
            try
            {
                if (e.Error == null)
                {
                    if (e.Result.Count > 0)
                    {
                        if (this.clientList != null)
                        {
                            this.clientList.Clear();
                        }

                        // 将服务器端的LIST转换为客户端的ObservableCollection
                        foreach (AccountInforBase item in e.Result)
                        {
                            if (!item.AccountID.ToString().Trim().ToUpper().Equals("ADMINISTRATOR"))
                            {
                                this.clientList.Add(new Client_AccountManagementDataList()
                                {
                                    AccountID = FunctionClass.MyIsNullOrEmpty(item.AccountID, null),

                                    // check默认值
                                    AccountIDCheck = false,
                                    AccountState = FunctionClass.MyIsNullOrEmpty(item.AccountState, null),
                                    OpName = FunctionClass.MyIsNullOrEmpty(item.OpName, null),
                                    ParentID = FunctionClass.MyIsNullOrEmpty(item.ParentID, null)
                                });
                            }
                        }

                        this.pcv = new PagedCollectionView(this.clientList);
                        this.pcv.PageSize = this.pageCount;

                        this.dgAMList.ItemsSource = this.pcv;
                        this.dpDataGridPage.DataContext = this.pcv;

                        this.wcfLogClient.InfoAsync(((App)Application.Current).LogStr + "在帐号管理项的子窗口中搜索记录");
                    }
                    else
                    {
                        this.dgAMList.DataContext = null;
                        this.dpDataGridPage.DataContext = null;
                        throw (new Exception("没有可显示的数据"));
                    }
                }
                else
                {
                    throw (new Exception(e.Error.Message.ToString()));
                }
            }
            catch (Exception ex)
            {
                this.wcfLogClient.ErrorAsync(((App)Application.Current).LogStr + "AccountManagementListShowChild" + ex.Message.ToString());

                Common.FunctionList.FunctionClass.ShowMessageBox(
                    this.messbox,
                    1,
                    "提示",
                    ex.Message.ToString(),
                    Colors.Gray,
                    true,
                    Visibility.Visible,
                    Visibility.Collapsed);
                return;
            }
        }
        #endregion 单击搜索框，向DATAGRID中添加信息        
    }
}

